/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.chain.shell;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.TreeSet;

/**
 *
 * @author eisental
 */
public class ShortcutObject extends ShellObject {

    public ShortcutObject(DriveShell shell) { super("shortcut", "shortcuts", "short", shell); }

    @Override
    public boolean isActionSupported(Action action) {
        return (action!=Action.set && action!=Action.inspect);
    }

    @Override
    public void make(String command) throws IllegalArgumentException {
        int colonIdx = command.indexOf(':');
        if (colonIdx==-1)
            throw new IllegalArgumentException("Invalid syntax for make shortcut. Expecting make shortcut <name>:<replacement>");

        String key = command.substring(0, colonIdx);
        String value = command.substring(colonIdx+1) + ";";
        shell.getShortcuts().setProperty(key, value);

        out.println("Added shortcut " + key + " >> " + value);
    }

    @Override
    public void list() throws IllegalArgumentException {
        printShortcuts(shell);
    }

    @Override
    public void remove(String command) throws IllegalArgumentException {
        if (shell.getShortcuts().containsKey(command)) {
            shell.getShortcuts().remove(command);
            out.println("Removed shell shortcut " + command);
        } else throw new IllegalArgumentException("Shell shortcut " + command + " does not exist.");
    }

    @Override
    public void set(String command) throws IllegalArgumentException {
    }

    @Override
    public void inspect(String command) throws IllegalArgumentException {
    }

    public void save(String command) throws IllegalArgumentException {
        out.println("Storing shortcuts...");

        String header = "#\n" +
                "# shell shortcuts\n" +
                "# ---------------\n" +"#\n" +
                "# driveshell shortcut definitions in Java properties format.\n" +
                "# see http://en.wikipedia.org/wiki/.properties for info about the syntax.\n" + "#\n" +
                "# each line should consist of a 'key: value' pair. The key is the command name of the shortcut and\n" +
                "# the value is its replacement string. %1, %2, and %% can be used in the value, to copy the 1st argument, \n" +
                "# the 2nd argument or all the arguments together respectively, from the shortcut command to the replacement. \n" +
                "# For example the shortcut definition 'goto: set active %1;' will translate the command 'goto root;' into \n" +
                "# 'set active root;'\n" + "#\n" +
                "# " + DateFormat.getDateInstance(DateFormat.LONG).format(Calendar.getInstance().getTime()) + " "
                + DateFormat.getTimeInstance(DateFormat.LONG).format(Calendar.getInstance().getTime()) + "\n\n";


        String props = "";
        for (String key : shell.getShortcuts().stringPropertyNames()) {
            props += key + ": " + shell.getShortcuts().getProperty(key) + "\n";
        }

        try {
            Writer output = new BufferedWriter(new FileWriter(DriveShell.SHORTCUTS_PATH));
            try {
                output.write(header + props + "\n");
            }
            finally {
                output.close();
            }
        } catch (IOException io) {
            throw new IllegalArgumentException("Error while storing shortcuts:" + io);
        }
    }

    static void printShortcuts(DriveShell shell) {
        TreeSet<String> sortShortcuts = new TreeSet<String>();
        if (shell.getShortcuts().stringPropertyNames().isEmpty()) {
            shell.getPrintStream().println(" - There are no registered shell shortcuts.");
        }

        for (String key : shell.getShortcuts().stringPropertyNames())
            sortShortcuts.add(key);

        for (String key : sortShortcuts)
            shell.getPrintStream().println(" * " + key + " >> " + shell.getShortcuts().getProperty(key));
    }

}
